<?php

if(stristr($_SERVER['PHP_SELF'],".inc.php")) { 
	header("Location:index.php"); 
	exit; 
}

$uploaddir = $svrRoot.'/smallaxe_icons/uploads/';

if($_POST['topic_add']) {
	unset($_errors); 

	if(!$_POST['topic_name']) {
		$_errors .= $Lang['errBlankFields'] . "<br />\n";
	} else { 
		$tname = stripslashes($_POST['topic_name']); 
	}
	
	if(!$_FILES) { 
		$fname = "default.png"; 
	} else { 
		$fname = basename($_FILES['topic_file']['name']);
		$uploadfile = $uploaddir."/".$fname; 
		$ftype 	= $_FILES['topic_file']['type'];
		if(!stristr($ftype,"image/")) { 
			echo "Problem uploading your attachment!\nError: Your attachment was not accepted because it appears 
			to be an invalid file type.  The file type was: ".$ftype."<br /><br />Use the upload files dialog 
			if you feel this is an error.\n";
			exit;				
		}
	
		if (move_uploaded_file($_FILES['topic_file']['tmp_name'], $uploadfile)) {
			$fsize	= $_FILES['topic_file']['size'];	   
		} elseif($_FILES['topic_file']['error']=="1") {
		   echo "Problem uploading your attachment!\n";
		   echo "Error: ";	   
		   echo "Your attachment is too large for the system! Max file size is 2 megabytes.\n";
		   exit;	
		} elseif($_FILES['topic_file']['error']=="2") {
		   echo "Problem uploading your attachment!\n";
		   echo "Error: ";	
		   echo "Your attachment is too large for this script! Max file size is 2 megabytes.\n";
		   exit;
		} elseif($_FILES['topic_file']['error']=="3") {
		   echo "Problem uploading your attachment!\n";
		   echo "Error: ";	
		   echo "No file was only partially uploaded.  Try again.\n";
		   exit;
		} elseif($_FILES['topic_file']['error']=="4") {
		   echo "Problem uploading your attachment!\n";
		   echo "Error: ";	
		   echo "No file was specified.\n";
		   exit;	   
		} else {
		   echo "Unknown problem uploading your attachment!\n";
		   //if($DEBUG) { 
		   echo "Debugging info:\n<pre>"; print_r($_FILES); echo "</pre>\n"; 
		  //} 
		   exit;
		}
	}
	
	if(!$_errors) {
		$timg = "uploads/".$fname; 
		new_topic($tname,$timg);  
		unset($goto); 
		$goto = $self."?op=topics&message=success"; 
	} else { 
		unset($goto); 
		$goto = $self."?op=topics&message=".$_errors; 
	}
	header("Location: ".$goto); 
	exit;
	
} elseif($_POST['topic_delete']) {
	unset($_errors); 
	if(!$_POST['topic']) {
		$_errors .= $Lang['errBlankFields'] . "<br />\n";
	} 
	$topic_id = intval($_POST['topic']);
	if(!$_errors) {
		delete_topic($topic_id); 
		$goto = $self."?op=topics&message=success"; 
	} else { 
		$goto = $self."?op=topics&message=".$_errors;  	
	}
	header("Location: ".$goto);
	exit; 


} elseif($_POST['topic_mod']) {
	unset($_errors); 
	if(!$_POST['topic_name']) {
		$_errors .= $Lang['errBlankFields'] . "<br />\n";
	} else { 
		$topic_rename = stripslashes($_POST['topic_name']); 
	}
	$topic_id = intval($_POST['topic']);
	$active=intval($_POST['active']);
	if(!$_errors) {
		change_topic($topic_id,$topic_rename,$active); 
		$goto = $self."?op=topics&message=success"; 
	} else { 
		$goto = $self."?op=topics&message=".$_errors; 
	}
	header("Location: ".$goto);
	exit; 
	
} elseif($subop == "add") { ?>
	
	<form action="<?=$self;?>" method="post" name="myform" enctype="multipart/form-data">
	<fieldset class="myfieldset">
		<legend>Add Topic</legend>
		<input type="hidden" name="op" value="topics" />
		Topic Name: <br />
		<input type="text" name="topic_name" class="text_input" size="50" /><br />
<?php if(is_writeable($uploaddir)) { ?>
		Topic Image: <br />
	    <input type="hidden" name="MAX_FILE_SIZE" value="20480000" />
		<input type="file" name="topic_file" size="50" class="text_input" /><br />
		Allowed Filetypes: <acronym title="JPEG Image">JPG</acronym>, <acronym title="GIF Image">GIF</acronym>, <acronym title="Bitmap Image">BMP</acronym>, <acronym title="Portable Network Graphics">PNG</acronym></small>
<?php } else { 
		echo "Your topics directory:<br /><br /><strong><i>".$uploaddir."</i></strong><br /><br />
		is not writable.  Your new topic's image will be the default icon:<br /><br />
		<img src=\"./smallaxe_icons/default.png\" alt=\"Your Topic's Name\" />\n";
} ?>	
	</fieldset>
	<br /><br />
	<input type="submit" name="topic_add" class="button" value="<?=$Lang['edit']." ".$Lang['topic'];?>" />&nbsp;&nbsp;
	<input type="button" name="cancel" class="button" value="<?=$Lang['noCancel'];?>" onclick="location.href='?'" />&nbsp;&nbsp;
	<br />
	</form>	

<?php } elseif($subop == "mod") { ?>

	<form action="<?=$self;?>?op=topics" method="post" name="myform" onsubmit="return confirm_delete_topic()" >
	<fieldset class="myfieldset">
		<legend><?php echo $Lang['edit']." ".$Lang['topic'];?></legend>
		<input type="hidden" name="op" value="topics" />
		Choose Topic: <br />
		<select name="topic" class="text_input" id="topic_id" onchange="javascript:topicSwap();">
<?php
	foreach($topics as $k=>$v) { 
		echo "\t\t<option value=\"".$v['topicid']."\">".$v['name']."</option>\n";
	}
?>	
		</select>
		<br /><br />
		Rename to: <br />
		<input type="text" name="topic_name" class="text_input" id="topic_rename" size="50" />
		<br /><br />
		Status: <br />
		<input type="radio" name="active" value="1" checked="checked">Active<br />
		<input type="radio" name="active" value="0">Inactive		
	</fieldset>
	<br /><br />
	<input type="submit" name="topic_mod" class="button" value="Make Changes" />&nbsp;&nbsp;
	<input type="button" name="cancel" class="button" value="<?=$Lang['noCancel'];?>" onclick="location.href='?op=topics'" />
	&nbsp;&nbsp;&nbsp;-- OR --&nbsp;&nbsp;&nbsp;	
	<input type="submit" name="topic_delete" class="button" onclick="javascript:this.clicked=true" value="<?=$Lang['delete']." ".$Lang['topic'];?>" id="topic_delete" />&nbsp;&nbsp;
	<br />
	</form>	

<?php } else { 
	
	if($_errors) { echo "<div class=\"message\">".$_errors."</div><br />\m"; }
	if($_GET['message'] && $_GET['message'] == "success") { echo "<div class=\"message\">Operation completed successfully!</div><br />\n"; }
	echo "<fieldset style=\"width:500px;margin-left:auto;margin-right:auto;text-align:left;\">
		<legend>Admin &gt; Topic Operations</legend>
			<a href=\"".$self."?op=topics&subop=add\">Add a New Topic</a><br />
			<a href=\"".$self."?op=topics&subop=mod\">Modify a Topic</a><br />
	</fieldset>";	
}
?>